package com.xinqi.modules.sys.textbook.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookInfoCreateDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookInfoQueryDTO;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookInfoUpdateDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookInfoResultDTO;
import com.xinqi.modules.sys.textbook.service.CloudTextbookInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 云课本基本信息ADMIN控制层
 *
 * @author: linkai
 * @date: 2023/04/25
 */
@Tag(name = "Admin-云课本", description = "云课本Admin接口")
@RestController
@RequestMapping("/admin/cloud-textbook/")
@RequiredArgsConstructor
public class CloudTextbookInfoAdminController {
    private final CloudTextbookInfoService cloudTextbookInfoService;

    /**
     * 云课本基本信息-分页
     */
    @Operation(summary = "云课本基本信息-分页")
    @GetMapping("/page")
    public R<PageInfo<CloudTextbookInfoResultDTO>> page(@ParameterObject CloudTextbookInfoQueryDTO dto) {
        PageInfo<CloudTextbookInfoResultDTO> page = cloudTextbookInfoService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 云课本基本信息-详情
     */
    @Operation(summary = "云课本基本信息-详情")
    @GetMapping("/{id}")
    public R<CloudTextbookInfoResultDTO> findById(@PathVariable("id") Long id) {
        CloudTextbookInfoResultDTO dto = cloudTextbookInfoService.findById(id);
        return R.data(dto);
    }


    @Operation(summary = "云课本名称是否已存在")
    @GetMapping("/is-exist-name")
    public R<Boolean> isExistName(@Parameter(description = "云课本名称", example = "一年级语文哭") String name,
                                  @Parameter(description = "当前修改的ID，新增不用填", example = "100001") Long oldId
    ) {
        return R.data(cloudTextbookInfoService.isExistName(name, oldId));
    }


    /**
     * 新增云课本基本信息
     */
    @Operation(summary = "新增云课本基本信息")
    @PostMapping
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增云课本", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@RequestBody CloudTextbookInfoCreateDTO dto) {
        return R.data(cloudTextbookInfoService.insert(dto));
    }

    /**
     * 修改云课本基本信息
     */
    @Operation(summary = "修改云课本基本信息")
    @PutMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改云课本", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@PathVariable("id") Long id, @RequestBody CloudTextbookInfoUpdateDTO dto) {
        dto.setId(id);
        return R.data(cloudTextbookInfoService.updateById(dto));
    }

    /**
     * 删除云课本基本信息
     */
    @Operation(summary = "删除云课本基本信息")
    @DeleteMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除云课本", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@PathVariable("id") Long id) {
        return R.data(cloudTextbookInfoService.deleteById(id));
    }

    @Operation(summary = "课本数据导入")
    @PostMapping("/import")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 导入云课本数据", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    public R<List<String>> upload(@RequestParam("file") MultipartFile file) {
        if (StringUtils.isEmpty(file.getOriginalFilename())) {
            return R.fail("文件不存在");
        }
        List<String> errorList = cloudTextbookInfoService.upload(file);
        if (!errorList.isEmpty()) {
            R<List<String>> r = R.error("导入数据有错误");
            r.setData(errorList);
            return r;
        }
        return R.ok();
    }

}
